# 导入json模块
# 导入哈希库
import hashlib
import json
import os
# 导入uuid类
import uuid

import openpyxl
# 导入Settings
from django.conf import settings
# 导入Q查询
from django.db.models import Q
# 引入JsonResponse模块
from django.http import JsonResponse
# 引入Student的类
from student.models import Student
from student.student_service import StudentService
#  引入分页器
from django.core.paginator import Paginator
from django.views.decorators.http import require_http_methods


# def get_students(request):
#     """获取所有学生信息"""
#     try:
#         # get data
#         result = StudentService.get_student()
#         # 返回
#         return JsonResponse({'code': 1, 'data': result})
#     except Exception as e:
#         # 如果出现异常，返回
#         return JsonResponse({'code': 0, 'msg': "获取学生信息出现异常，具体错误：" + str(e)})


@require_http_methods(["POST"])
def student_list(request):
    """获取当前页学生信息"""
    try:
        # 获取请求体中的数据
        data = json.loads(request.body.decode('utf-8'))
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误'})

    try:
        # 调用 Service 层获取分页后的学生列表
        response_data = StudentService.get_students_with_pagination(data)
        return JsonResponse(response_data)
    except Exception as e:
        return JsonResponse({'code': 0, 'msg': str(e)})


@require_http_methods(["POST"])
def query_students(request):
    """实现学生信息查询"""
    try:
        # 获取请求体中的数据
        data = json.loads(request.body.decode('utf-8'))
    except json.JSONDecodeError:
        return JsonResponse({
            'code': 0,
            'msg': '请求体格式错误，无法解析JSON数据'
        })

    # 调用 Service 层查询学生信息
    response_data = StudentService.query_students(data)
    return JsonResponse(response_data)


@require_http_methods(["POST"])
def is_exists_sno(request):
    try:
        data = json.loads(request.body.decode('utf-8'))
        result = StudentService.is_exists_sno(data)
        return JsonResponse(result)
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误，无法解析JSON数据'})


@require_http_methods(["POST"])
def add_student(request):
    try:
        data = json.loads(request.body.decode('utf-8'))
        result = StudentService.add_student(data)
        return JsonResponse(result)
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误，无法解析JSON数据'})
    except Exception as e:
        return JsonResponse({'code': 0, 'msg': str(e)})


@require_http_methods(["POST"])
def update_student(request):
    try:
        data = json.loads(request.body.decode('utf-8'))
        result = StudentService.update_student(data)
        return JsonResponse(result)
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误，无法解析JSON数据'})


@require_http_methods(["POST"])
def delete_student(request):
    try:
        data = json.loads(request.body.decode('utf-8'))
        result = StudentService.delete_student(data)
        return JsonResponse(result)
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误，无法解析JSON数据'})
def delete_students(request):
    try:
        # 获取请求体中的数据
        data = json.loads(request.body.decode('utf-8'))
        result = StudentService.delete_students(data)
        return JsonResponse(result)
    except json.JSONDecodeError:
        return JsonResponse({'code': 0, 'msg': '请求体格式错误，无法解析JSON数据'})


@require_http_methods(["POST"])
def upload(request):
    rev_file = request.FILES.get('avatar')
    result = StudentService.upload_file(rev_file)
    return JsonResponse(result)

@require_http_methods(["POST"])
def import_students_excel(request):
    rev_file = request.FILES.get('excel')
    result = StudentService.import_students_excel(rev_file)
    return JsonResponse(result)


@require_http_methods(["GET"])
def export_student_excel(request):
    """导出学生数据到Excel"""
    result = StudentService.export_students_excel()
    return JsonResponse(result)
